package com.xzk.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzk.bean.BootStrapTableUser;
import com.xzk.bean.Message;
import com.xzk.bean.ResultData;
import com.xzk.bean.User;
import com.xzk.exception.DuplicateIdCardException;
import com.xzk.exception.DuplicatePhoneException;
import com.xzk.service.UserService;
import com.xzk.util.DateFormatUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CourierController
 * @Description 用户
 * @Author 0715-YuHao
 * @Date 2020/9/14 16:08
 * @Version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource(name = "userService")
    private UserService service;

    @RequestMapping("/console.do")
    public @ResponseBody Message console() {
        Map<String, Integer> data = service.console();
        Message msg = new Message();
        if (data.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(data);
        return msg;
    }

    @RequestMapping("/list.do")
    public @ResponseBody ResultData<BootStrapTableUser> list(HttpServletRequest request) {
        //1. 获取分页查询数据的起始索引
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //2. 获取当前页要查询的数据量
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        //3. 进行查询
        PageHelper.startPage(pageNumber, pageSize);
        List<User> list = service.findAll();
        PageInfo<User> info = new PageInfo<>(list);
        List<BootStrapTableUser> list2 = new ArrayList<>();
        int i = 1;
        for (User user : info.getList()) {
            int status = user.getStatus();
            String showStatus = null;
            switch (status) {
                case 0:
                    showStatus = "未认证";
                    break;
                case 1:
                    showStatus = "已删除";
                    break;
                case 2:
                    showStatus = "已认证";
                    break;
                default:
            }
            list2.add(new BootStrapTableUser(
                    i++,
                    user.getNickName(),
                    user.getPhone(),
                    user.getIdCard(),
                    "*******",
                    showStatus,
                    DateFormatUtil.formatDate(user.getSignUpTime()),
                    user.getLoginTime() == null ? "未登录" : DateFormatUtil.formatDate(user.getLoginTime())
            ));
        }
        //4. 将集合封装为 bootstrap-table识别的格式
        ResultData<BootStrapTableUser> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(info.getTotal());
        return data;
    }

    @RequestMapping("/insert.do")
    public @ResponseBody Message insert(HttpServletRequest request) {
        String nickName = request.getParameter("nickName");
        String phone = request.getParameter("phone");
        String idCard = request.getParameter("idCard");
        String password = request.getParameter("password");
        Message message = new Message();
        try {
            boolean insert = service.insert(new User(nickName, phone, idCard, password));
            if (insert) {
                message.setStatus(0);
                message.setContent("注册成功");
            }else {
                message.setStatus(-1);
                message.setContent("注册失败");
            }
        } catch (DuplicateIdCardException e) {
            message.setStatus(-1);
            message.setContent("身份证已被注册！");
        } catch (DuplicatePhoneException e) {
            message.setStatus(-1);
            message.setContent("手机号已被注册！");
        }
        return message;
    }

    @RequestMapping("/find.do")
    public @ResponseBody Message find(HttpServletRequest request) {
        String phone = request.getParameter("phone");
        User user = service.findByPhone(phone);
        Message message = new Message();
        if (user == null) {
            message.setStatus(-1);
            message.setContent("手机号码不存在");
        }else {
            message.setStatus(0);
            message.setContent("查询成功");
            message.setData(user);
        }
        return message;
    }

    @RequestMapping("/update.do")
    public @ResponseBody Message update(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        String nickName = request.getParameter("nickName");
        String phone = request.getParameter("phone");
        String idCard = request.getParameter("idCard");
        String password = request.getParameter("password");
        Message message = new Message();
        try {
            boolean update = service.update(id, new User(nickName, phone, idCard, password));
            if (update) {
                message.setStatus(0);
                message.setContent("修改成功");
            }else {
                message.setStatus(-1);
                message.setContent("修改失败");
            }
        } catch (DuplicatePhoneException e) {
            message.setStatus(-1);
            message.setContent("手机号已被注册！");
        } catch (DuplicateIdCardException e) {
            message.setStatus(-1);
            message.setContent("身份证已被注册！");
        }
        return message;
    }

    @RequestMapping("/delete.do")
    public @ResponseBody Message delete(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        String phone = request.getParameter("phone");
        String idCard = request.getParameter("idCard");
        Message message = new Message();
        boolean delete = service.delete(id, phone, idCard);
        if (delete) {
            message.setStatus(0);
            message.setContent("删除成功");
        }else {
            message.setStatus(-1);
            message.setContent("删除失败");
        }
        return message;
    }
}
